package org.link.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import org.link.util.UserAllData;
import org.link.util.Util;

/**
 * 
 * project_name:jksystem
 * package_name:org.link.ui
 * file_name:ExamFrame.java
 * @author Link
 * create_date:2015年9月20日下午2:29:06
 * modified_date:
 * description: 模拟考试窗体类
 *
 */
public class ExamFrame extends JFrame {

	private JPanel mainPane;			//主面板
	private JButton frontButton;		//上一题按钮
	private JButton referButton;		//提交按钮
	private JButton nextButton;			//下一题按钮
	private JPanel upPanel;				//上面版
	private JLabel showTimeLabel;		//时间显示标签
	private JLabel userInfoLabel;		//考生信息标签
	private JPanel questionPanel;		//题目按钮面板
	private JButton questionButton;		
	private JRadioButton aRadioButton;	//A选项按钮
	private JRadioButton bRadioButton;	//B选项按钮
	private JRadioButton cRadioButton;	//C选项按钮
	private JRadioButton dRadioButton;	//D选项按钮
	private JLabel tittleLabel;			//题目标签
	private ButtonGroup buttonGroup;
	private JLabel imageLabel;			//图片标签
	private int num = 1;				//题目编号
	private RadioButtonMouseListener rbml = new RadioButtonMouseListener();
	private QuesBtnActionListener qbal = new QuesBtnActionListener();
	private RadioButtonActionListener rbal = new RadioButtonActionListener();
	private Date date;
	private double score;
	private Timer timer;
	
	/**
	 * Create the frame.
	 */
	public ExamFrame() {
		setSize(763, 479);
		Util.screen(this);
		setResizable(false);
		setTitle("小车_科目一_模拟考试");
		mainPane = new JPanel();
		mainPane.setBackground(Color.WHITE);
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPane.setBounds(0, 0, 763, 479);
		getContentPane().add(mainPane);
		mainPane.setLayout(null);
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				ExamFrame.this.setVisible(false);
				UserAllData.questionButtons.clear();
				timer.stop();
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		/**
		 * 上一题按钮
		 */
		frontButton = new JButton("上一题");
		frontButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		frontButton.setBounds(363, 256, 99, 38);
		mainPane.add(frontButton);
		frontButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(num == 1){
					JOptionPane.showMessageDialog(null, "已是第一题");
					return;
				}else{
					num--;
					updateUpPanel(num-1); //调用更新面板方法
				}
			}
		});
		/**
		 * 下一题
		 */
		nextButton = new JButton("下一题");
		nextButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		nextButton.setBounds(497, 256, 99, 38);
		mainPane.add(nextButton);
		nextButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(num == 25){
					JOptionPane.showMessageDialog(null, "已是最后一题");
					return;
				}else{
					num++;
					updateUpPanel(num-1); //调用更新面板方法
				}
			}
		});
		
		/**
		 * 提交按钮
		 */
		referButton = new JButton("提 交");
		referButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		referButton.setBounds(637, 256, 99, 38);
		mainPane.add(referButton);
		referButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int num = JOptionPane.showConfirmDialog(null, "交卷提示",",考试时间未结束,是否确认提交?",JOptionPane.YES_NO_OPTION);
				if(JOptionPane.YES_OPTION==num){
					JOptionPane.showMessageDialog(null, "考试结束,本次考试成绩: "+score);
					UserAllData.questionButtons.clear();
					ExamFrame.this.setVisible(false);
				}
			}
		});
		
		upPanel = new JPanel();
		upPanel.setBounds(0, 0, 760, 245);
		upPanel.setLayout(null);
		
		tittleLabel = new JLabel();
		tittleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		tittleLabel.setBounds(10, 10, 600, 49);
		tittleLabel.setText("1. "+UserAllData.userExamPapers.get(0).getQuesTittle());
		upPanel.add(tittleLabel);
		
		buttonGroup = new ButtonGroup();
		/**
		 * A选项按钮
		 */
		aRadioButton = new JRadioButton("A");
		aRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		aRadioButton.setBounds(47, 83, 480, 30);
		aRadioButton.setText("A: "+UserAllData.userExamPapers.get(0).getQuesOptionA());
		upPanel.add(aRadioButton);
		buttonGroup.add(aRadioButton);
		//aRadioButton.addActionListener(rbal);
		aRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(aRadioButton.isSelected()){
					if(num == 25){
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("A")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
					}else{
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						updateUpPanel(num);
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("A")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
						num++;
					}
					
				}
			}
		});
		aRadioButton.addMouseListener(rbml);
		
		/**
		 * B选项按钮
		 */
		bRadioButton = new JRadioButton("B");
		bRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		bRadioButton.setBounds(47, 121, 480, 30);
		bRadioButton.setText("B: "+UserAllData.userExamPapers.get(0).getQuesOptionB());
		upPanel.add(bRadioButton);
		buttonGroup.add(bRadioButton);
		//bRadioButton.addActionListener(rbal);
		bRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(bRadioButton.isSelected()){
					if(num == 25){
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("B")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
					}else{
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						updateUpPanel(num);
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("B")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
						num++;
					}
					
				}
				
			}
		});
		bRadioButton.addMouseListener(rbml);
		/**
		 * C选项按钮
		 */
		cRadioButton = new JRadioButton();
		cRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		cRadioButton.setBounds(47, 163, 480, 30);
		buttonGroup.add(cRadioButton);
		//cRadioButton.addActionListener(rbal);
		cRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cRadioButton.isSelected()){
					if(num == 25){
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("C")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}	
						}
					}else{
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						updateUpPanel(num);
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("C")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}	
						}
						num++;
					}
					
				}
				
			}
		});
		cRadioButton.addMouseListener(rbml);
		/**
		 * D选项按钮
		 */
		dRadioButton = new JRadioButton();
		dRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		dRadioButton.setBounds(47, 209, 480, 30);
		//dRadioButton.addActionListener(rbal);
		dRadioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(dRadioButton.isSelected()){
					if(num == 25){
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("D")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
					}else{
						try {
							Thread.sleep(500);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						buttonGroup.clearSelection();
						updateUpPanel(num);
						if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals("D")){
							score+=4.0;
							UserAllData.questionButtons.get(num-1).setBackground(Color.green);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}else{
							UserAllData.questionButtons.get(num-1).setBackground(Color.red);
							questionPanel.removeAll();
							questionPanel.repaint();
							for (JButton btn : UserAllData.questionButtons) {
								questionPanel.add(btn);
							}
						}
						num++;
					}
					
				}
				
			}
		});
		dRadioButton.addMouseListener(rbml);
		buttonGroup.add(dRadioButton);
		if(UserAllData.userExamPapers.get(0).getQuesOptionC()!=null){
			cRadioButton.setText("C: "+UserAllData.userExamPapers.get(0).getQuesOptionC());
			dRadioButton.setText("D: "+UserAllData.userExamPapers.get(0).getQuesOptionD());
			upPanel.add(dRadioButton);
			upPanel.add(cRadioButton);
		}
		
		imageLabel = new JLabel();
		imageLabel.setBounds(530, 45, 235, 175);
		upPanel.add(imageLabel);
		if(UserAllData.userExamPapers.get(0).getImageAddress()!=null){
			ImageIcon tp = new ImageIcon(ExamFrame.class.getResource(UserAllData.userExamPapers.get(0).getImageAddress()));
			imageLabel.setIcon(tp);
		}
		
		mainPane.add(upPanel);
		
		showTimeLabel = new JLabel("考试剩余时间: 45:00");
		showTimeLabel.setBounds(20, 260, 160, 30);
		showTimeLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
		showTimeLabel.setForeground(Color.blue);
		mainPane.add(showTimeLabel);
		
		date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		try {
			date = sdf.parse("45:00");
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 timer = new Timer(1000,new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(date.getTime()==-28799000){
					showTimeLabel.setText("考试剩余时间: 00:00");
					timer.stop();
					JOptionPane.showMessageDialog(null, "考试时间到,考试成绩: "+score);
					UserAllData.questionButtons.clear();
					ExamFrame.this.setVisible(false);
				}else{
					date = new Date(date.getTime()-1000);
					try {
						String str = new SimpleDateFormat("mm:ss").format(date);
						showTimeLabel.setText("考试剩余时间: "+str);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		timer.start();
		
		userInfoLabel = new JLabel("考生姓名: "+UserAllData.userExamPapers.get(0).getUserName());
		userInfoLabel.setBounds(190, 260, 150, 30);
		userInfoLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
		mainPane.add(userInfoLabel);
		
		
		questionPanel = new JPanel();
		questionPanel.setBounds(20, 320, 720, 120);
		questionPanel.setLayout(new GridLayout(3, 10, 10, 10));
		for(int i = 1;i<26;i++){
			questionButton = new JButton(i+"");
			questionButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
			questionButton.addActionListener(qbal);//添加事件监听内部类
			UserAllData.questionButtons.add(questionButton);
			questionPanel.add(questionButton);
		}
		mainPane.add(questionPanel);
		
	}
	
	/**
	 * 
	 * project_name:jksystem
	 * package_name:org.link.ui
	 * file_name:ExamFrame.java
	 * @author Link
	 * create_date:2015年9月21日 下午12:36:56
	 * modified_date:
	 * description: 选项按钮事件监听类
	 *
	 */
	class RadioButtonActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			JRadioButton jrb = (JRadioButton) e.getSource();
			//System.out.println(jrb.getText().charAt(0));
			if(jrb.isSelected()){
				if(num == 25){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					buttonGroup.clearSelection();
					System.out.println((UserAllData.userExamPapers.get(num-1).getRightAnswer()));
					if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals(jrb.getText().charAt(0))){
						score+=4.0;
						UserAllData.questionButtons.get(num-1).setBackground(Color.green);
						questionPanel.removeAll();
						questionPanel.repaint();
						for (JButton btn : UserAllData.questionButtons) {
							questionPanel.add(btn);
						}
					}else{
						UserAllData.questionButtons.get(num-1).setBackground(Color.red);
						questionPanel.removeAll();
						questionPanel.repaint();
						for (JButton btn : UserAllData.questionButtons) {
							questionPanel.add(btn);
						}
					}
				}else{
					try {
						Thread.sleep(500);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					buttonGroup.clearSelection();
					updateUpPanel(num);
					if(UserAllData.userExamPapers.get(num-1).getRightAnswer().equals(jrb.getText().charAt(0))){
						score+=4.0;
						UserAllData.questionButtons.get(num-1).setBackground(Color.green);
						questionPanel.removeAll();
						questionPanel.repaint();
						for (JButton btn : UserAllData.questionButtons) {
							questionPanel.add(btn);
						}
					}else{
						UserAllData.questionButtons.get(num-1).setBackground(Color.red);
						questionPanel.removeAll();
						questionPanel.repaint();
						for (JButton btn : UserAllData.questionButtons) {
							questionPanel.add(btn);
						}
					}
					num++;
				}
			}
		}	
	}
		
	
	
	/**
	 * 
	 * description: 更新题目信息面板公共方法
	 * creatTime:2015年9月20日下午10:56:44
	 * @author Link
	 * return type: void
	 *
	 */
	public void updateUpPanel(int num){
		upPanel.removeAll();
		upPanel.repaint();
		tittleLabel.setText((num+1)+": "+UserAllData.userExamPapers.get(num).getQuesTittle());
		aRadioButton.setText("A: "+UserAllData.userExamPapers.get(num).getQuesOptionA());
		bRadioButton.setText("B: "+UserAllData.userExamPapers.get(num).getQuesOptionB());
		upPanel.add(aRadioButton);
		upPanel.add(bRadioButton);
		upPanel.add(tittleLabel);
		if(UserAllData.userExamPapers.get(num).getQuesOptionC()!=null){
			cRadioButton.setText("C: "+UserAllData.userExamPapers.get(num).getQuesOptionC());
			dRadioButton.setText("D: "+UserAllData.userExamPapers.get(num).getQuesOptionD());
			upPanel.add(cRadioButton);
			upPanel.add(dRadioButton);
		}
		if(UserAllData.userExamPapers.get(num).getImageAddress()!=null){
			ImageIcon tp = new ImageIcon(ExamFrame.class.getResource(UserAllData.userExamPapers.get(num).getImageAddress()));
			imageLabel.setIcon(tp);
			upPanel.add(imageLabel);
		}
	}
	
	/**
	 * 
	 * project_name:jksystem
	 * package_name:org.link.ui
	 * file_name:ExamFrame.java
	 * @author Link
	 * create_date:2015年9月21日 上午9:13:49
	 * modified_date:
	 * description: 公共鼠标监听事件
	 *
	 */
	class RadioButtonMouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			JRadioButton jRadioButton = (JRadioButton) e.getSource();
			jRadioButton.setSelected(true);
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	
	/**
	 * 
	 * project_name:jksystem
	 * package_name:org.link.ui
	 * file_name:ExamFrame.java
	 * @author Link
	 * create_date:2015年9月21日 上午9:43:25
	 * modified_date:
	 * description: questionButton按钮事件监听内部类
	 *
	 */
	class QuesBtnActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			JButton jButton = (JButton) e.getSource();
			int num = Integer.parseInt(jButton.getText());
			updateUpPanel(num-1);
			ExamFrame.this.num = num;
		}
	}
	

}





